﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AsyncCallback.AsyncEvent
{
    static class EventsHelper
    {
        delegate void AsyncFire(Delegate del, object[] args);

        public static void FireAsync(Delegate del, params object[] args)
        {
            if (del == null) { return; }
            Delegate[] delegate_list = del.GetInvocationList();
            AsyncFire asyncFire = InvokeDelegate;
            foreach (Delegate sink in delegate_list)
            {
                asyncFire.BeginInvoke(sink, args, null, null);
            }
        }

        static void InvokeDelegate(Delegate del, object[] args)
        {
            del.DynamicInvoke(args);
        }
    }

static class EventsHelper2
{
    delegate void AsyncFire(Delegate del, object[] args);

    private static void UnsafeFireAsync(Delegate del, params object[] args)
    {
        if (del == null) { return; }
        Delegate[] delegate_list = del.GetInvocationList();
        AsyncFire asyncFire = InvokeDelegate;
        foreach (Delegate sink in delegate_list)
        {
            asyncFire.BeginInvoke(sink, args, null, null);
        }
    }

    private static void InvokeDelegate(Delegate del, object[] args)
    {
        del.DynamicInvoke(args);
    }

    public static void FireAsync(GernericEventHandler<int> del,int number)
    {
        UnsafeFireAsync(del, number);
    }

    public static void FireAsync<T>(GernericEventHandler<T> del, T t)
    {
        UnsafeFireAsync(del, t);
    }

    public static void FireAsync<T,U>(GernericEventHandler<T,U> del, T t,U u)
    {
        UnsafeFireAsync(del, t,u);
    }
}
}
